package cn.iocoder.yudao.module.infra.api.file;

import cn.iocoder.yudao.module.infra.api.file.dto.CreateFileResDTO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.infra.service.file.UploadFileResult;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import jakarta.annotation.Resource;

/**
 * 文件 API 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class FileApiImpl implements FileApi {

    @Resource
    private FileService fileService;

    @Override
    public String createFile(String name, String path, byte[] content) {
        return fileService.createFile(name, path, content);
    }

    @Override
    public String createFile(String name, String path, byte[] content, Long configId) {
        return fileService.createFile(name, path, content, configId).getFileUrl();
    }

    @Override
    public String getPresignedObjectUrl(String path, String method, Integer expirySec) {
        try {
            return fileService.getPresignedObjectUrl(path, method, expirySec);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public CreateFileResDTO onlyCreateFile(String name, String path, byte[] content) {
        final UploadFileResult res = fileService.onlyCreateFile(name, path, content, null);
        return CreateFileResDTO.builder()
                .url(res.getFileUrl())
                .encryptKey(res.getEncryptKey())
                .build();
    }

    @Override
    public String getTempDownloadUrl(String encryptKey, String path, Integer resourceDurationSec) {
        return fileService.getTempDownloadUrl(encryptKey, path, resourceDurationSec);
    }

}
